package com.tlxch.server.general.Impl;

import com.tlxch.config.DeepseekConfig;
import com.tlxch.deepseek.ChatRequest;
import com.tlxch.deepseek.ChatResponse;
import com.tlxch.deepseek.Message;
import com.tlxch.pojo.DeepSeek;
import com.tlxch.server.general.DeepSeekService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@Service
public class DeepSeekServiceImpl implements DeepSeekService {

    @Autowired
    DeepseekConfig deepseekConfig;

    public DeepSeek call(String message) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json");
        headers.add("Accept", "application/json");
        headers.add("Authorization", "Bearer " + deepseekConfig.getKey());

        ChatRequest request = new ChatRequest();
        request.setModel("deepseek-chat");
        Message msg = new Message("user", message);
        List<Message> list = new ArrayList<>();
        list.add(msg);
        request.setMessages(list);

        HttpEntity<ChatRequest> entity = new HttpEntity<>(request, headers);
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<ChatResponse> response = restTemplate.exchange(deepseekConfig.getUrl(), HttpMethod.POST, entity, ChatResponse.class);

        //获取原始数据
        String originalResponse = response.getBody().getChoices().get(0).getMessage().getContent();
        System.out.println(originalResponse);
        DeepSeek deepSeek = new DeepSeek();
        deepSeek.setContent(originalResponse.substring(2));
        deepSeek.setFenshu(Double.parseDouble(originalResponse.substring(0,1)));
        deepSeek.setTsLevel(Integer.valueOf(originalResponse.substring(1,2)));
        return deepSeek;
    }
}
